// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    index_desc: app.globalData.index_desc,
    is_over: app.globalData.is_over,
    uid: app.globalData.uid,
  },
  // 事件处理函数
  bindViewTap() {
    wx.redirectTo({
      url: '../logs/logs'
    })
  },
  onLoad() {
    this.wechat_login()
  },

  wechat_login() {
    const p_l = this.pre_load
    var that = this
    var remote_ip = app.globalData.remote_ip
    wx.login({
      
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        if (res.code) {
          //发起网络请求
          wx.request({
            url: remote_ip + 'wechat_login',
            method: 'POST',
            data: {
              code: res.code
            },
            success(res) {
              const rs = res.data.data

        
              getApp().globalData.uid = rs.openid
              getApp().globalData.is_over = rs.is_over
              getApp().globalData.index_desc = rs.index_desc
              var svr_code= rs.code

              that.setData({
                'uid': rs.openid,
                'is_over': rs.is_over,
                'index_desc': rs.index_desc,
              });

              p_l(svr_code)
            },
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },

  pre_load(svr_code) {
    // # 0 跳转到选择模式
    // # 1 跳转至任务描述
    var url_link = ""
    switch (svr_code) {
      case 0:
        url_link = "/pages/select_mode/select_mode"
        break;
      case 1:
        url_link = "/pages/ready/ready"
        break;
      default:
        url_link = "/pages/select_mode/select_mode"
        break;
    }
    var timeout = 2000;
    var timer_id = setTimeout(function () {
      wx.redirectTo({
        url: url_link,
      })
      clearTimeout(timer_id);
    }, timeout)
  },

})